//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by niqiang on 16/6/30.
//  Copyright © 2016年 niqiang. All rights reserved.
//

import UIKit

private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
//    //微博数据数组
//    private lazy var statusList = [String]()
//列表视图模型
    private lazy var listViewModel = WBStatusListViewModel()
    
    //加载数据
    override func loadData() {
//        print("开始加载数据\(WBNetworkManager.shared)")
 
        //——————————————————1
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = ["access_token": "2.00IzHKDD02sthJae72419026nkdmlD"]
        
//        WBNetworkManager.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
//            print(json)
//            }) { (_, error) in
//                print("网络请求失败\(error)")
//        }

//        WBNetworkManager.shared.request(URLString: urlString, parameters: params) { (json, isSuccess) in
        //——————————————————2
//        WBNetworkManager.shared.statusList { (list, isSuccess) in
//            print(list)
//        }
//        
//        
//        //模拟"延时"加载数据
//        DispatchQueue.main.after(when: DispatchTime.now() + 1){
//        
//            for i in 0..<15 {
//                
//                if self.isPullup {
//                
//                self.statusList.append("上拉 \(i)")
//                }else{
//                
//                    //将数据插入到数组的顶部
//                    //at:表示插入到数组的第”0“个元素前
//                    self.statusList.insert(i.description, at: 0)
//                }
//            }
        
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess) in
           
            print("加载数据结束")
            //恢复上拉刷新标记
            self.isPullup = false
            
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            
            //刷新表格(无此步刷新按钮界面不显示数据)
            self.tableView?.reloadData()
        }
//  对比1      listViewModel.loadStatus(completion: (isSuccess: Bool) -> ())
//  对比2      listViewModel.loadStatus { (isSuccess) in
//            code
//        }
        print("加载中")
        
        }
    
    
    
    //显示好友
    @objc private func showFriends(){
        print(#function)
        
        let vc = WBDemoViewController()
        
        //vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

//MARK: - 表格数据源方法具体实现
extension WBHomeViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        return cell
        
    }
}

//MARK: - 设置界面

extension WBHomeViewController {
    
    //重写父类方法
//    override func setupUI() {
//        super.setupUI()
    
    override func setupTableView() {
        super.setupTableView()
    
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        //
        //        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray(), highlightedColor: UIColor.orange())
        //        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
    
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupNavTitle()
    }
    
    //设置导航栏标题
    private func setupNavTitle() {
//        let button = UIButton.cz_textButton("哟呵呵", fontSize: 17, normalColor: UIColor.darkGray(), highlightedColor: UIColor.black())
        let title = WBNetworkManager.shared.userAccount.screen_name
        
        
//        button?.setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
//        button?.setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        let button = WBTitleButton(title: title)
        
        
        navItem.titleView = button
        
//        button?.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
    
        //设置选中状态
        btn.isSelected = !btn.isSelected
    }
}
